import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.LayoutStyle.ComponentPlacement;

@SuppressWarnings("serial")
public class GUI extends JFrame {
	
	public static JFrame frame;
	public JPanel panel;
	public JButton buttonSend, buttonExit, buttonChoose;
	public JLabel label, labelFile;
	public static JProgressBar progressBar;
	public File file = null;

	public GUI() {
		setTitle("Client");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(400, 200, 550, 200);
		
		panel = new JPanel();
		setContentPane(panel);
		
		label = new JLabel("Welcome to the program that can send your file to the server.");
		label.setForeground(Color.BLUE);
		Font font = new Font("Verdana", Font.ITALIC, 16);
		label.setFont(font);
		
		buttonSend = new JButton("Send");
		buttonSend.setVisible(false);
		buttonSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Client(file).execute();
			}
		});

		buttonChoose = new JButton("Choose file");
		buttonChoose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				int result = fileChooser.showDialog(null, "Choose file");                
				if (result == JFileChooser.APPROVE_OPTION) {
					file = fileChooser.getSelectedFile();
					labelFile.setText(file.getName());
					labelFile.setVisible(true);
					progressBar.setVisible(true);
					buttonSend.setVisible(true);
				}
			}
		});
		
		progressBar = new JProgressBar();
		progressBar.setVisible(false);
		progressBar.setStringPainted(true);
		progressBar.setMinimum(0);
		progressBar.setMaximum(100);
		progressBar.setValue(0);
		
		labelFile = new JLabel("Name of file");
		labelFile.setVisible(false);
		
		
		buttonExit = new JButton("Exit");
		buttonExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(EXIT_ON_CLOSE);
			}
		});

		GroupLayout groupLayout = new GroupLayout(panel);
        groupLayout.setHorizontalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
             	.addGroup(groupLayout.createSequentialGroup()
             		.addContainerGap()
             		.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
             			.addGroup(groupLayout.createSequentialGroup()
             				.addComponent(label, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(50))
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                            	.addGroup(groupLayout.createSequentialGroup()
                            		.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
                            			.addComponent(buttonSend, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(buttonChoose, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                        	.addGap(50)
                                            .addComponent(labelFile)
                                            .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                            .addGap(40)
                                            .addComponent(progressBar, GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE))))
                                .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
                                	.addGap(1)
                                    .addComponent(buttonExit)))
                            .addGap(50))))
        );
        groupLayout.setVerticalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
            	.addGroup(groupLayout.createSequentialGroup()
            		.addContainerGap()
                    .addComponent(label, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                    	.addComponent(buttonChoose)
                        .addComponent(labelFile))
                    .addGap(20)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(progressBar, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(buttonSend))
                    .addGap(20)
                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(buttonExit, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(40))
        );
		panel.setLayout(groupLayout);
	}
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				GUI frame = new GUI();
				frame.setVisible(true);
			}
		});
	}
}

